package com.yigang.homecontroller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.yigang.entity.Product;
import com.yigang.entity.ProductVO;
import com.yigang.entity.Shoppingcart;
import com.yigang.entity.User;
import com.yigang.service.ShopingCartService;
import com.yigang.utils.UserConst;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.druid.stat.TableStat.Mode;
import com.yigang.service.ProductService;

@Controller
public class ProductListController {

    @Autowired
    ProductService productService;

    @Autowired
    private ShopingCartService shopingCartService;

    @RequestMapping("/getProductList")
    public String getProductList(Model model, HttpSession session, @RequestParam("product_id") int product_id) {
//        List<Map<String, Object>> list = productService.getProductList(product_id);
        List<Map<String, Object>> list1 = productService.productRecommend();
        List<Map<String, Object>> list2 = productService.productRecommend1();
        List<Map<String, Object>> list3 = productService.productRecommend2();
//        System.out.println(list);
        System.out.println(list1);
        System.out.println(list2);
        System.out.println(list3);
//        model.addAttribute("list", list);
        model.addAttribute("c", list1);
        model.addAttribute("list2", list2);
        model.addAttribute("list3", list3);
//        model.addAttribute("list",new ArrayList());
        model.addAttribute("list1", list1);
        model.addAttribute("list2", list2);
        model.addAttribute("list3", list3);
//        Product product = productService.getById(product_id);
        ProductVO product = productService.getColorById(product_id);
        model.addAttribute("product", product);
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            Shoppingcart shoppingcart =  shopingCartService.getByProductIdAndUserId(user.getId(),product.getProductId());
            model.addAttribute("shoppingcart", shoppingcart);
        }

        return "jsp/home/product";

    }
}
